﻿using ArcGIS.Core.Data;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using CCTool.Scripts.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ArcGIS.Desktop.Framework.Dialogs;
using MessageBox = ArcGIS.Desktop.Framework.Dialogs.MessageBox;
using CCTool.Scripts.ToolManagers;
using CCTool.Scripts.ToolManagers.Extensions;
using CCTool.Scripts.ToolManagers.Managers;

namespace CCTool.Scripts.Attribute.FieldString
{
    /// <summary>
    /// Interaction logic for GetPinyin.xaml
    /// </summary>
    public partial class GetPinyin : ArcGIS.Desktop.Framework.Controls.ProWindow
    {
        public GetPinyin()
        {
            InitializeComponent();
            Init();
        }

        // 初始化
        private void Init()
        {
            combox_model.Items.Add("1-全拼，无音标");
            combox_model.Items.Add("2-全拼，带数字音标");
            combox_model.Items.Add("3-全拼，带字母音标");
            combox_model.Items.Add("4-首字母，小写");
            combox_model.Items.Add("5-首字母，大写");
            combox_model.SelectedIndex = 4;
        }

        // 定义一个进度框
        private ProcessWindow processwindow = null;
        string tool_name = "汉字转拼音";

        private void combox_fc_DropDown(object sender, EventArgs e)
        {
            UITool.AddFeatureLayerAndTableToComboxPlus(combox_fc);
        }

        private void combox_field_in_DropDown(object sender, EventArgs e)
        {
            UITool.AddTextFieldsToComboxPlus(combox_fc.ComboxText(), combox_field_in);
        }

        private void combox_field_out_DropDown(object sender, EventArgs e)
        {
            UITool.AddTextFieldsToComboxPlus(combox_fc.ComboxText(), combox_field_out);
        }

        private async void btn_go_click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 获取数据
                string layer_path = combox_fc.ComboxText();
                string field_in = combox_field_in.ComboxText();
                string field_out = combox_field_out.ComboxText();
                string model = combox_model.Text;
                int modelIndex = model[..1].ToInt();

                // 判断参数是否选择完全
                if (layer_path == "" || field_in == "" || field_out == "")
                {
                    MessageBox.Show("有必选参数为空！！！");
                    return;
                }

                // 打开进度框
                ProcessWindow pw = UITool.OpenProcessWindow(processwindow, tool_name);
                pw.AddMessageTitle(tool_name);

                Close();

                await QueuedTask.Run(() =>
                {
                    pw.AddMessageStart($"转换模式：{model}");

                    // 字段计算
                    using Table table = layer_path.TargetTable();
                    using RowCursor rowCursor = table.Search();


                    long totalCount = table.GetCount();
                    int count = 1;
                    while (rowCursor.MoveNext())
                    {
                        if (count % 1000 == 0)
                        {
                            pw.AddMessageMiddle(count / totalCount * 80, $"处理行数：{count}/{totalCount}.......");
                        }

                        using Row row = rowCursor.Current;
                        // 获取输入字段的值
                        string value_in = row[field_in]?.ToString();
                        if (value_in is not null)
                        {
                            row[field_out] = BaseTool.ChineseToPinyin(value_in, modelIndex);
                        }
                        // 保存
                        row.Store();
                        count++;
                    }
                });
                pw.AddMessageEnd();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + ee.StackTrace);
                return;
            }
        }

        private void btn_help_click(object sender, RoutedEventArgs e)
        {
            string url = "https://blog.csdn.net/xcc34452366/article/details/148852118";
            UITool.Link2Web(url);
        }
    }
}
